﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Configuration;

using SIPO.Library.DataAccess.Administration.UserManagement;
using SIPO.Library.DomainObject.Administration.UserManagement;
using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.DataAccess.Administration.Catalog;

using SIPO.Web.Support;

namespace SIPO.Web.Forms.UserManagement
{
    public partial class sUserform : BasePage
    {
        private int id = -1;
        private int status = -1;
        
        protected override void OnLoad(EventArgs e)
        {
            hfStatus.Value = status.ToString();
            txtUserCode.Focus();
            if (Request.Params["Id"] != null)
            {
                id = Convert.ToInt32(Request.Params["Id"]);
            }
            if (Request.Params["Status"] != null)
            {
                status = Convert.ToInt32(Request.Params["Status"]);
                hfStatus.Value = status.ToString();
            }
            if (!Page.IsPostBack)
            {
                recDebtorRepo Deptordal = new recDebtorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);

                List<recDebtor> Debtors = Deptordal.GetList();

                Debtors.ForEach(delegate(recDebtor debtor)
                {
                    ddlDebtor.Items.Add(new ListItem(debtor.DebtorCode+ " - " + debtor.DebtorName, debtor.DebtorID.ToString()));
                });

                ddlDebtor.Items.Insert(0, new ListItem("-- pilih debitur --", string.Empty));

                if (status == 1)
                {
                    tr1.Visible = false;
                }
                else {
                    tr1.Visible = true;
                }
                
                if (id > 0) // Edit an existing item
                {
                    txtUserName.Focus();
                    recUserRepo userDal = new recUserRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                    recUser dto = userDal.GetById(id);

                    if (dto != null)
                    {
                        hfID.Value = id.ToString();
                        txtUserName.Text = dto.UserName;
                        txtUserCode.Text = dto.UserCode;
                        txtEmail.Text = dto.Email;
                        txtMobile.Text = dto.Mobile;
                        chkIsActive.Checked = dto.IsActive.HasValue ? dto.IsActive.Value : false;
                        ddlDebtor.SelectedValue = dto.DebtorID.ToString();
                        trPassword.Visible = false;
                        trConfirmPassword.Visible = false;
                        txtUserCode.ReadOnly = true;
                    }
                }

               // GetPrivilege();

            }
        }

        private void GetPrivilege()
        {
            Membership membership = new Membership(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);

            List<recAccessRights.AccessRights> accessRights = membership.GetAccessRights(base.UserID, base.MenuID);

            accessRights.ForEach(delegate(recAccessRights.AccessRights access)
            {
                switch (access)
                {
                    case (recAccessRights.AccessRights.View):
                        ClientScript.RegisterStartupScript(typeof(Page), "view", "$('#btnSave').button('disable');", true);
                        break;
                    case (recAccessRights.AccessRights.Add):
                        ClientScript.RegisterStartupScript(typeof(Page), "add", "$('#btnSave').button('enable');", true);
                        break;
                    case (recAccessRights.AccessRights.Edit):
                        ClientScript.RegisterStartupScript(typeof(Page), "edit", "$('#btnSave').button('enable');", true);
                        break;
                    case (recAccessRights.AccessRights.NoAccess):
                        var url = Helper.GetAppVirtualPath(Page);
                        ClientScript.RegisterStartupScript(typeof(Page), "noAccess", "alert('Anda tidak mempunyai hak akses ke halaman ini!'); window.location = '" + url + "'", true);
                        break;
                }
            });

        }
    
    }
}

